package ReflectTest04;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

/*
练习二：通过配置文件运行类中的方法
 */
public class ReflectDmeo02 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//        Student s=new Student();
//        s.study();

//        Teacher t=new Teacher();
//        t.teach();

        /*
          class.txt
          className=xxx
          methodName=xxx
         */

        //加载数据
        Properties prop = new Properties();
        FileReader fr = new FileReader("D:\\javajhx\\fs\\src\\ReflectTest04\\class.txt");
        prop.load(fr);
        fr.close();
        /*
        className=ReflectTest04.Student
        methodName=study
         */
        //通过键获取值 
        String className = prop.getProperty("className");
        String methodName = prop.getProperty("methodName");

        //通过反射来使用
        Class<?> c = Class.forName(className);//ReflectTest04.Student
        Constructor<?> con = c.getConstructor();//获取无参构造方法
        Object obj = con.newInstance();

        Method m = c.getMethod(methodName);//study
        m.invoke(obj);
    }
}
